#!/bin/bash

# 设置环境变量
export PROJ_ROOT=/home/apachee/trunk/Crystal/CrystalFlowAnalyzer/a9soc

echo "Processing DWC_usb3.lst file by file..."

# 读取文件列表并逐个处理
counter=1
while IFS= read -r line; do
    # 跳过注释行和空行
    if [[ -z "$line" ]] || [[ "$line" == //* ]] || [[ "$line" == \#* ]] || [[ "$line" == component:* ]] || [[ "$line" == library:* ]] || [[ "$line" == files:* ]]; then
        echo "Skipping line: $line"
        continue
    fi
    
    echo "Processing file #$counter: $line"
    
    # 创建单文件列表
    echo "$line" > "file_$counter.lst"
    
    # 运行moparse处理单个文件
    echo "Running: ./moparse -f file_$counter.lst -o file_$counter.modb"
    ./moparse -f "file_$counter.lst" -o "file_$counter.modb"
    
    # 检查是否成功
    if [ -f "file_$counter.modb" ]; then
        size=$(wc -c < "file_$counter.modb")
        echo "SUCCESS: Generated file_$counter.modb ($size bytes)"
    else
        echo "FAILED: file_$counter.modb was not generated"
        break
    fi
    
    # 只处理前5个文件进行测试
    if [ $counter -eq 5 ]; then
        echo "Stopping after 5 files for testing purposes"
        break
    fi
    
    counter=$((counter + 1))
done < "/home/apachee/trunk/Crystal/CrystalFlowAnalyzer/a9soc/design/usb2/src/DWC_usb3.lst"

echo "Processing complete"